package com.work.keep.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.datas.DynamicDiscoverData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class MyFollowsRecyclerAdapter extends RecyclerView.Adapter<MyFollowsRecyclerAdapter.MyFollowsViewHolder> {
    private Context context;
    private List<DynamicDiscoverData.DataEntity> discoverDataList;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private OnRecyclerItemClickListener onRecyclerItemClickListener;

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener) {
        this.onRecyclerItemClickListener = onRecyclerItemClickListener;
    }

    public MyFollowsRecyclerAdapter(Context context) {
        this.context = context;
        this.discoverDataList = new ArrayList<>();
        init();
    }

    public MyFollowsRecyclerAdapter(Context context, List<DynamicDiscoverData.DataEntity> discoverDataList) {
        this.context = context;
        this.discoverDataList = discoverDataList;
        init();
    }

    private void init() {
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton.getImageLoader();
    }

    public void addFollowsDataList(List<DynamicDiscoverData.DataEntity> discoverDataList) {
        this.discoverDataList = discoverDataList;
        notifyDataSetChanged();
    }

    @Override
    public MyFollowsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.fragment_followings_item, parent, false);
        MyFollowsViewHolder holder = new MyFollowsViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyFollowsViewHolder holder, final int position) {
        bindNetWorkImageView(holder, position);
        bindAvaterNetWorkIV(holder, position);
        bindFollowsContent(holder, position);
        holder.followsName.setText(discoverDataList.get(position).getAuthor().getUsername());
        holder.followsDate.setText(discoverDataList.get(position).getCreated().substring(0,10));
        holder.followsLikesCount.setText(discoverDataList.get(position).getLikes() + "");
        holder.followsCommentCounts.setText(discoverDataList.get(position).getComments() + "");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRecyclerItemClickListener != null) {
                    onRecyclerItemClickListener.onItemClick(position);
                }
            }
        });
    }

    private void bindFollowsContent(MyFollowsViewHolder holder, int position) {
        if (discoverDataList.get(position).getContent().length() > 0) {
            holder.followsContent.setText(discoverDataList.get(position).getContent());
        }else {
            holder.followsContent.setVisibility(View.GONE);
        }
    }

    //avater
    private void bindAvaterNetWorkIV(MyFollowsViewHolder holder, int position) {
        holder.followsAvater.setErrorImageResId(R.mipmap.ic_launcher);
        holder.followsAvater.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.followsAvater.setImageUrl(discoverDataList.get(position).getAuthor().getAvatar(), imageLoader);
    }

    //shared photo
    private void bindNetWorkImageView(final MyFollowsViewHolder holder, int position) {
        if (discoverDataList.get(position).getPhoto().length() > 0) {
            holder.followsNI.setDefaultImageResId(R.mipmap.ic_launcher);
            holder.followsNI.setErrorImageResId(R.mipmap.ic_launcher);
            holder.followsNI.setImageUrl(discoverDataList.get(position).getPhoto(), imageLoader);
            ImageRequest imageRequest = new ImageRequest(discoverDataList.get(position).getPhoto(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    Palette.Builder builder = Palette.from(response);
                    builder.generate(new Palette.PaletteAsyncListener() {
                        @Override
                        public void onGenerated(Palette palette) {
                            Palette.Swatch virbant = palette.getVibrantSwatch();
                            if (virbant != null) {
                                holder.followsLayout.setBackgroundColor(virbant.getRgb());
                                holder.color = virbant.getRgb();
                            }
                        }
                    });
                }
            }, 40, 40, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            volleySingleton._addRequest(imageRequest);
        } else {
            holder.followsNI.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return discoverDataList.size();
    }

    public class MyFollowsViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout followsLayout;
        CardView followsCardView;
        NetworkImageView followsAvater;
        TextView followsName, followsDate, followsContent, followsLikesCount, followsCommentCounts;
        NetworkImageView followsNI;
        int color;

        public int getColor() {
            return color;
        }

        public MyFollowsViewHolder(View itemView) {
            super(itemView);
            followsAvater = (NetworkImageView) itemView.findViewById(R.id.followsAvater);
            followsLayout = (RelativeLayout) itemView.findViewById(R.id.followsLayout);
            followsCardView = (CardView) itemView.findViewById(R.id.followsCardView);
            followsName = (TextView) itemView.findViewById(R.id.followsName);
            followsDate = (TextView) itemView.findViewById(R.id.followsDate);
            followsContent = (TextView) itemView.findViewById(R.id.followsContent);
            followsLikesCount = (TextView) itemView.findViewById(R.id.followsLikesCount);
            followsCommentCounts = (TextView) itemView.findViewById(R.id.followsCommentCounts);
            followsNI = (NetworkImageView) itemView.findViewById(R.id.followsNI);
            int color = 0;
        }
    }
}
